<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
		<a-form-item
		        :labelCol="labelCol"
		        :wrapperCol="wrapperCol"
		        label="生产状态名称"
		        hasFeedback required>
		        <a-input  style="width: 200px;" placeholder="请输入生产状态名称" v-model="dataname" />

		         <!-- // v-show="xianshi" -->
		      </a-form-item>



      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="品类"
          hasFeedback required>

  <a-select
        v-model="categoryId"
       style="width: 200px"
      @focus="focus"
      ref="select"
      @change="handleChange"
      placeholder="请选择品类"
    >
       <a-select-option v-for="item in categoryList"  :value="item.id.toString()" :key="item.id">
            {{item.category_name}}
            </a-select-option>
  </a-select>

        </a-form-item>


  <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="编码"
          hasFeedback >
          <a-input style="width: 200px;" placeholder="请输入编码"   v-model="code" />

           <!-- // v-show="xianshi" -->
        </a-form-item>



          <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
          hasFeedback >
          <a-input  style="width: 200px;" type="number" placeholder="请输入排序" v-model="sortint" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
import { deleteAction, getAction, putAction, postAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
  export default {
    name: "addProductionStatus",
    data () {
      return {
		  id:'',
		  code:'',
		  sortint:'',
		  dataname:'',
		  categoryId:'请选择品类',
        recoilType:'',  //反冲类型
        userId:'',
        recoilMoney:'',//反冲金额
        factoryResponsibility:'',//工厂责任
        platformResponsibility:'',//平台责任
        remark:'', //备注
        title:"操作",
        visible: false,
        xianshi:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/ProductionStatus/addProductionStatus",
          edit: "/ProductionStatus/addProductionStatus",
		    pingleilist:"/Discount/getallcategory",
			detail:"/ProductionStatus/detail",
			update:"/ProductionStatus/updateProductionStatus"
        },
      }
    },
    created () {
      this.getallcategory();
    },
    methods: {
		//查询所有品类
		    getallcategory(){
		     getAction(this.url.pingleilist).then((res)=>{
		        if(res.success){
		         this.categoryList=res.result;
		        }else{
		          that.$message.warning(res.message);
		        } })
		},

		//查询详情
		    getdetail(){
		     getAction(this.url.detail,{id:this.id}).then((res)=>{
		        if(res.success){
				     this.dataname=res.result.dataname;
		         this.categoryId=res.result.categoryId;
		         this.code=res.result.code;
		         this.sortint=res.result.sort;
		        }else{
		          this.$message.warning(res.message);
		        } })
		},
      add () {
        this.edit({});
      },
      edit (record) {
        this.dataname='';
        this.categoryId='';
        this.code='';
        this.sortint='0';
        this.id=record;
        if(record!=0){
          this.getdetail();
        }
        this.form.resetFields();
        this.model[0] = Object.assign({}, record);
         // this.model = record;
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model[0],'userId'))

        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
		    if(this.dataname==null || this.dataname==''){
		      this.$message.warning("请输入生产状态名称")
          return
        }
        if(this.categoryId==null || this.categoryId=='请选择品类'){
          this.$message.warning("请选择品类")
          return;
        }

        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(this.id==0){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.update;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);
              formData['dataname']=this.dataname;
              formData['categoryId']=this.categoryId;
	          formData['code']=this.code;
	          formData['sort']=this.sortint;
			      formData['id']=this.id;
            postAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('aaa');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>